//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { scrollMixin } from "utils/mixin";
export default {
  name:'',
  //import引入的组件需要注入到对象中才能使用
  components: {  },
  mixins: [scrollMixin], //加载更多
  data() {
  return {
    active:0,
    ruleShow:false,
    checked:false,
    polyInfo:{},
    categoryId:'',
    title:'',
    page: 1,
    isLoadMore: true,
    total_page: 0,
    groupList:[],
    presentTime:'',
    swiperOption: {
      autoplay: false,
      // loop: true,   loop模式第二轮循环的时候第一张会闪烁
      delay: 3000,
      observer: true,
      pagination: {
        el: ".swiper-pagination"
      }
    }
  };
  },
  //监听属性 类似于data概念
  computed: {

  },
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    beforeDay(time) {
      //结束时间减去前一天
      return (time * 1000) - Number(this.presentTime);
    },
    startDay(time) {
      return (time * 1000) - Number(this.presentTime);
    },
    computingTime(afterTime) {
      if(afterTime) {
        if(Number(this.presentTime) < (afterTime * 1000)){
          return true
        } else {
          return false;
        }
      }
    },
    endTime(time) {
      if(Number(this.presentTime) > (time * 1000)){
        return true
      } else {
        return false;
      }
    },
    endDayChange(index) {
      this.groupList.splice(index, 1);
    },
    diyNav() {
      if(this.polyInfo.diy_button_url) {
        window.location.href = this.polyInfo.diy_button_url;
      }
    },
    slideNavTo(url) {
      if(url) {
        window.location.href = url;
      }
    },
    takeNav() {
      this.$router.push(this.fun.getUrl("customTakeList"));
    },
    groupNav(group) {
      this.$router.push(this.fun.getUrl("customNameList",{groupId:group.id},{title:group.group_name}));
    },
    ruleConfirm() {
      if(this.fun.isTextEmpty(this.checked)) {
        this.$toast('请勾选同意规则');
        return false;
      }
      this.ruleShow = false;
      this.agreeRule();
    },
    categoryTab(event) {
      this.categoryId = event;
      this.page = 1;
      this.groupData();
    },
    agreeRule() { //记录用户有没有确认弹窗,只请求
      $http.post("plugin.customized-group.frontend.controllers.index.agree-rule", {basic_info:1}, "正在获取").then(
        res => {
          console.log(res);
        },
        function(response) {
          // console.log(response);
          // error callback
        });
    },
    polyIndex() {
      $http.post("plugin.customized-group.frontend.controllers.index.index", {basic_info:1}, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.title = response.basic_info?.home?.mailInfo?.name;
            this.polyInfo = res;
            this.polyInfo.rule.desc =res.rule?.desc?.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"');
            this.categoryId = res.category[0].id;
            this.ruleShow = res.rule?.is_show;
            if(this.title) {
              this.fun.setWXTitle(this.title);
            }
            this.groupData();
          } else {
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    groupData() {
      $http.get('plugin.customized-group.frontend.controllers.group.group-list', {page:this.page,category_id:this.categoryId}, "加载中..").then(response => {
        if (response.result == 1) {
            let res = response.data;
            let list = res.list.data;
            this.isLoadMore = true;
            this.total_page = res.list.last_page;
            if (!this.total_page) {
              this.page = 0;
            }
            this.groupList = list;
        } else {
          this.page = this.page - 1;
          this.isLoadMore = false;
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        $http.post("plugin.customized-group.frontend.controllers.group.group-list", {page:this.page,category_id:this.categoryId}, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let data = res.data;
              let list = data.list.data;
              this.groupList = [...this.groupList, ...list]; //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.polyIndex();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {

  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    this.presentTime = new Date().getTime();
    this.active = 0;
    this.page= 1;
    
  }, //如果页面有keep-alive缓存功能，这个函数会触发
  }